package com.nian.servlet;

import com.nian.pojo.User;
import com.nian.service.UserService;
import com.nian.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/admin/admin.do")
public class AdminServlet extends BaseServlet {
    public void login(HttpServletRequest req, HttpServletResponse resp){
        UserService userService = new UserServiceImpl();
        String username = req.getParameter("inputEmail");
        String password = req.getParameter("inputPassword");
        String remember = req.getParameter("remember-me");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        User login = userService.login(user);
        if (login!=null){
            if (remember!=null){
                resp.addCookie(new Cookie("username",username));
                try {
                    resp.sendRedirect("./product_list.jsp");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                resp.addCookie(new Cookie("username",""));
                try {
                    resp.sendRedirect("./product_list.jsp");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }else{
            req.setAttribute("flag","1");
        }
    }
    public void quit(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        session.invalidate();
        try {
            resp.sendRedirect("./login.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
